$(function () {

    /*查看分类信息*/
    $(".trContent .see").on("click", function () {
        $("#showHiden").hide().html("");
        $("#myModalLabel").html("");
        $(".modal-body").html("");
        var classifyName = $(this).parent().parent().find(".classifyName").html();
        $.ajax({
            url:"/api/classify/see",
            type:"get",
            data:{
                classifyName:classifyName
            },
            dataType:"json",
            success:function (data) {
                if(data.code==200){
                    var content = "<table class='table table-bordered table-hover'>" +
                                        "<tr>" +
                                            "<td>分类ID</td>" +
                                            "<td>" + data.messages[0]._id + "</td>" +
                                        "</tr>" +
                                        "<tr>" +
                                             "<td>分类名称</td>" +
                                             "<td>" + data.messages[0].classifyName + "</td>" +
                                        "</tr>" +
                                    "</table>";
                    $(".modal-body").html(content);



                }else{
                    $("#myModalLabel").html("<p>"+data.messages+"</p>");
                }
            }
        });


    });
    /*编辑分类信息*/
    $(".trContent .edit").on("click", function () {
        $("#showHiden").show().html("保存").attr("data-id","preserve");
        $("#myModalLabel").html("");
        $(".modal-body").html("");
        var cPar= $(this).parent().parent().find(".classifyName");
        var classifyName = $(this).parent().parent().find(".classifyName").html();

        $.ajax({
            url:"/api/classify/see",
            type:"get",
            data:{
                classifyName:classifyName
            },
            dataType:"json",
            success:function (data) {
                if(data.code==200){
                    var content = "<table class='table table-bordered table-hover'>" +
                            "<tr>" +
                                "<td>分类ID</td>" +
                                "<td>" + data.messages[0]._id + "</td>" +
                            "</tr>" +
                            "<tr>" +
                                "<td>分类名称</td>" +
                                "<td class='editSon'><input type value='"+ data.messages[0].classifyName+"'></td>" +
                            "</tr>" +
                        "</table>";
                    $(".modal-body").html(content);
                    
                    $(".editSon input").on("focus",function () {
                        $("#myModalLabel").html("");
                    });

                    var updataEle=$(".editSon input");
                    var oldVal=$(updataEle).val();
                    updataClassify(oldVal,updataEle,cPar);
                }else{
                    $("#myModalLabel").html("<p>"+data.messages+"</p>");
                }
            }
        });

    });
    function updataClassify(oldVal,updateEle,cPar) {
        $("#showHiden").on("click",function () {
            if($(updateEle).val()!=""){
                $.ajax({
                    url:"/api/classify/edit",
                    type:"post",
                    data:{
                        oldVal:oldVal,
                        updataVal:$(updateEle).val()
                    },
                    dataType:"json",
                    success:function (data) {
                        if(data.code==200){
                            $("#myModalLabel").html("<p>修改成功！</p>");
                            cPar.html($(updateEle).val())
                        }else{
                            $("#myModalLabel").html("<p>修改失败！</p>");
                        }
                    }
                })
            }else{
                $("#myModalLabel").html("<p>修改不能为空！</p>");

            }
        });


    }
    


    /*删除一个分类*/
    $(".trContent .del").on("click",function () {
        $("#showHiden").show().html("确认").attr("data-id","confirm");
        $("#myModalLabel").html("");
        $(".modal-body").html("您确认要删除这个用户吗？删除无法找回！");
        var cPar= $(this).parent().parent().find(".classifyName");
        var delVal=$(cPar).html();
        /*console.log(delVal);*/
        $("#showHiden").on("click",function () {
             $.ajax({
             url:"/api/classify/del",
             data:{
             delVal:delVal
             },
             dataType:"json",
             success:function (data) {
                 if(data.code==200){
                     $("#myModalLabel").html("<p>"+data.messages+"</p>");
                     setTimeout(function () {
                         location.href="/admin/classify";
                     },500)
                 }else{
                     $("#myModalLabel").html("<p>"+data.messages+"</p>");
                 }
             }

             })
        });


    });

});